import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.IOUtils;

public class TestRecognizer {

	public static void main(String[] args) throws Exception {
		new TestRecognizer().doTest();
	}

	StringBuilder buffer = new StringBuilder();

	ScatterDataRecognizer recognizer = new ScatterDataRecognizer();

	public TestRecognizer() throws Exception {
		init(recognizer);
	}

	public void doTest() {
		List<int[][]> regions = recognizer.recognize();
		printResult(regions);
	}

	byte[][] data;

	public void init(ScatterDataRecognizer recognizer) throws Exception {
		recognizer.setXAbsentAllowedCount(0);
		recognizer.setYAbsentAllowedCount(0);
		recognizer.setXOffsetAllowedCount(2);
		recognizer.setYOffsetAllowedCount(2);
		// load data
		InputStream is = getClass().getResourceAsStream("data.txt");
		List<String> list = IOUtils.readLines(is);
		data = new byte[list.size()][list.get(0).trim().toCharArray().length];
		byte VAL_POS = 1;
		// init data,positive-value and negative-value
		recognizer.init(data);
		char CHAR_POS = Character.forDigit(VAL_POS, 10);
		for (int l = 0; l < list.size(); l++) {
			char[] line = list.get(l).trim().toCharArray();
			if (line.length == 0) {
				throw new RuntimeException("Empty line!!!");
			} else if (line.length != data[0].length) {
				throw new RuntimeException("length of line not equal.");
			}
			int p = 0;
			for (char c : line) {
				if (c == CHAR_POS) {
					data[l][p] = VAL_POS;
				}
				p++;
			}
		}

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				if (data[i][j] == VAL_POS) {
					buffer.append("*");
				} else {
					buffer.append("_");
				}
			}
			buffer.append("\r\n");
		}
	}

	private String symbols = "!@#$%^&=+~,.";

	public void printResult(List<int[][]> regions) {
		int regionCount = 1;
		for (int[][] region : regions) {
			region[1][0] = region[0][0];
			region[1][1] = region[0][1];
			for (int[] range : region) {
				System.out.println(Arrays.toString(range));
				// +2 is in consideratioin of '\r\n' in each line of buffer
				// show edge points of region
				char c;
				if (regionCount < 10) {
					c = ("" + regionCount).charAt(0);
				} else {
					c = symbols.charAt((regionCount - 10)%symbols.length());
				}
				buffer.setCharAt((data[0].length + 2) * range[2] + (range[0]),
						c);
				buffer.setCharAt((data[0].length + 2) * range[2] + (range[1]),
						c);
			}
			System.out.println("------------------------------------");
			regionCount++;
		}
		System.out.println(buffer);
	}
}
